<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table @row-dblclick="getClick" :data="dictionarys" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="产品编号" width="120" >
      </el-table-column>
      <el-table-column prop="name" label="产品名称" width="180" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  抽屉  -->
    <el-drawer
        title="我嵌套了表格!"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table @row-dblclick="getClick" :data="detail" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="产品类型" width="120" >
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="handleAdd2(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleEdit2(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel2(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange2" :page-size="pageSize" :current-page="currentPage2" :total="total2" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="产品编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 抽屉编辑   -->
    <el-dialog :title="title" :visible.sync="editFormVisible2" :close-on-click-modal="false">
      <el-form :model="editForm2" label-width="80px" :rules="editFormRules2" ref="editForm">
        <el-form-item label="产品名称" prop="sn">
          <el-input v-model="editForm2.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="产品类型">
          <el-select v-model="editForm2.dictionary" value-key="id" placeholder="请选择">
            <el-option
                v-for="item in cities2"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible2 = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit2" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>



<script>

import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      filters2: {
        keyword: ''
      },
      details:[],
      table: false,
      gridData: [],
      detail:{},
      cities:[],
      cities2:[],
      id:[],
      dictionarys: [],
      total: 0,
      total2:0,
      currentPage:1,
      currentPage2:1,
      pageSize: 5,
      listLoading: false,
      sels: [],//列表选中列
      editFormVisible: false,//编辑界面是否显示
      editFormVisible2:false,
      editLoading: false,
      editFormRules: {
        sn: [
          { required: true, message: '请输入编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入品牌', trigger: 'blur' }
        ]

      },
      editFormRules2: {
        name: [
          { required: true, message: '请输入品牌', trigger: 'blur' }
        ], typesId: [
          { required: true, message: '请输入品牌', trigger: 'blur' }
        ]

      },
      //编辑界面数据
      editForm: {
        id:null,
        sn:'',
        name:''
      },
      editForm2: {
        id:null,
        name:"",
        typesId:null,
        dictionary:{}
      },
      title:"",
      dictionary:{}
    }
  },
  methods: {
    //获取部门列表
    getDictionarys() {
      let para = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      this.$http.post("/dictionary", para)
          .then(result => {
            // 关闭忙等
            this.listLoading = false;
            this.dictionarys = result.data.rows;
            this.total = result.data.total;
          })
          .catch(result => {
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            })
          })
    },

    getDetail(){
      this.$http.get("/dictionary")
          .then(result=>{
            this.cities2 = result.data
          })
    },


    getClick(row){
      this.id = row.id;
      this.currentPage2 = 1;
      this.table = true;
      let para = {
        pageSize:5,
        currentPage2: this.currentPage2,
        id:this.id
      };
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/detail",para)
          .then(result=>{
            this.listLoading = false
            this.detail = result.data.rows
            this.total2 = result.data.total
          })
          .catch(result=>{
        this.$message({
          message: '操作失败',
          type: 'error'
        });
      })
      this.getDetail();
    },




    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },

    search(){
      this.currentPage = 1;
      this.getDictionarys();
    },


    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getDictionarys();
    },

    handleCurrentChange2(currentPage2) {
      this.currentPage2 = currentPage2;
      let para = {
        pageSize:5,
        currentPage2: this.currentPage2,
        id:this.id
      };
      console.log(para)
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/detail",para)
          .then(result=>{
            this.listLoading = false
            this.detail = result.data.rows
            this.total2 = result.data.total
          }).catch(result=>{
        this.$message({
          message: '操作失败',
          type: 'error'
        });
      })
    },


    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.currentPage = 1;
        this.$http.delete("/dictionary/"+row.id)
            .then(result=>{
              if (result.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '提交失败',
                  type: 'error'
                });
              }

              this.listLoading = false;
              this.getDetail();
            }).catch(result=>{
          this.$message({
            message: '提交失败',
            type: 'error'
          });

        });

        this.$http.delete("/dictionary/del/"+row.id)
                .then(result=>{
                  if (result.data.success){
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '提交失败',
                      type: 'error'
                    });
                  }

                  this.listLoading = false;
                  this.getDetail();
                }).catch(result=>{
              this.$message({
                message: '提交失败',
                type: 'error'
              });

            });
      }).catch(() => {

      });
    },


    handleDel2: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.currentPage2 = 1;
        this.$http.delete("/detail/"+row.id)
            .then(result=>{
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: '操作失败',
                  type: 'error'
                });
              }
              this.listLoading = false;
              this.table = false;
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      }).catch(() => {//点击了确认框取消按钮

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.editFormVisible = true;
      //数据回显  Object.assign  克隆数据
      this.editForm = Object.assign({}, row);

    },
    handleEdit2: function (index, row) {
      this.title = "编辑";
      this.editFormVisible2 = true;
      //数据回显  Object.assign  克隆数据
      this.editForm2 = Object.assign({}, row);
      let dir = [];

    },

    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      // 在点击新增页面时修改title为编辑
      this.editFormVisible = true;
      this.editForm = {
        id:null,
        sn:'',
        name:''
      };
    },
    handleAdd2: function (index, row) {
      this.title = "新增";
      // 在点击新增页面时修改title为编辑
      this.editFormVisible2 = true;
      this.editForm2 = Object.assign({}, row);
      let dir = [];
    },


    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            // 从模型层editForm中克隆一个新对象
            let para = Object.assign({}, this.editForm);
            this.$http.put("/dictionary", para)
                .then(result=>{
                  // 关闭按钮忙等
                  this.editLoading = false;
                  // 关闭模态框
                  this.editFormVisible = false;
                  result = result.data;
                  if(result.success){ // 新增/修改成功
                    this.$message({
                      message: result.message,
                      type: 'success'

                    });
                  }else { // 不一定新增/修改成功，我们也不知道新增/修改成功没有
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  }
                  // 改变当前页页码
                  this.currentPage = 1;
                  // 调用查询列表函数
                  this.getDictionarys();
                })
                .catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: '系统繁忙，请重试！',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    editSubmit2: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = {
              id:this.editForm2.id,
              name: this.editForm2.name,
              typesId:this.editForm2.dictionary.id

            };
            console.log(this.editForm2)
            this.$http.put("/detail",para)
                .then(result=>{
                  if (result.data.success){
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '提交失败',
                      type: 'error'
                    });
                  }
                  this.editLoading = false;
                  this.editFormVisible2 = false;
                  this.table = false;
                })
                .catch(result=>{
                  this.$message({
                    message: '提交失败',
                    type: 'error'
                  });
                })
              });
            }
          });
        },



    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/dictionary", ids)
            .then(result=>{
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.currentPage = 1;
                this.getDictionarys();
              }else{
                this.$message({
                  message: '操作失败',
                  type: 'error'
                });
                this.currentPage = 1;
                this.getDictionarys();
              }
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    }
  },

  mounted() {
    this.getDictionarys();
    this.getDetail();
  }
}



</script>

<style scoped>

</style>